﻿Public Class FrmStageLigne
    Public pCD_CLI As Integer

    Private szSQL As String
    Private rs As ADODB.Recordset
    Dim oldCD_CLI As Integer

    Private Sub FrmStageLigne_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Me.Text = LibelleStage(gTypePlanning) & " - " & gSemaine & " - " & gGroupe & " - " & pCD_CLI
        InitComboStatutsLecon(CB_STATUT)
        InitComboMoniteur(CB_MONITEUR)
        ' utilisateur secretaire, autoriser suppression
        If gUserAcces < 6 Then
            BO_SUPPRIMER.Visible = False
        Else
            BO_SUPPRIMER.Visible = True
        End If
        InscritToScreen()

    End Sub

    Private Sub InscritToScreen()

        ' Lire la fiche du stage
        szSQL = "CALL spGet_PLA_STAGE('" & gTypePlanning & "'," & gSemaine & ",'" & gGroupe & "')"
        GetRecordset(ctDSN_AEM, szSQL, rs)
        If rs.RecordCount > 0 Then
            TE_DEBUT.Value = IfDate(rs.Fields("debut").Value)
            TE_FIN.Value = IfDate(rs.Fields("fin").Value)
            rs.Close()
        End If

        If pCD_CLI = 0 Then
            ' creation 
            ClearTextBoxes(Me)
            BO_ENREGISTRER.Enabled = True
            BO_RECHERCHER.Enabled = True
        Else
            ' modification, Lire la  ligne planning 
            szSQL = "CALL spGet_PLA_STAGE_LIGNES('" & gTypePlanning & "'," & gSemaine & ",'" & gGroupe & "'," & pCD_CLI & ")"
            GetRecordset(ctDSN_AEM, szSQL, rs)
            If rs.RecordCount > 0 Then

                TE_CD_CLI.Text = rs.Fields("cd_cli").Value
                oldCD_CLI = TE_CD_CLI.Text
                TE_NOM.Text = nv(rs.Fields("nom").Value)
                TE_FORMULE.Text = rs.Fields("formule").Value
                TE_LIEU.Text = nv(rs.Fields("lieu").Value)
                CB_MONITEUR.SelectedIndex = CB_MONITEUR.FindString(nv(rs.Fields("cd_mon").Value.ToString))
                CB_STATUT.SelectedIndex = CB_STATUT.FindString(rs.Fields("statut").Value.ToString)
                TE_QTE.Text = rs.Fields("qte").Value
                TE_REF.Text = nv(rs.Fields("ref").Value)
                TE_FIN_RESA.Text = IfDate(rs.Fields("fin_reservation").Value)
                TE_MODIFIE_LE.Text = setModifiedBy(rs.Fields("cree_le").Value, rs.Fields("modif_le").Value, rs.Fields("cd_uti").Value)
                If nv(rs.Fields("valide").Value) = "O" Or gUserAcces < 2 Then
                    BO_ENREGISTRER.Enabled = False
                    BO_SUPPRIMER.Enabled = False
                    BO_RECHERCHER.Enabled = False
                Else
                    BO_ENREGISTRER.Enabled = True
                    BO_SUPPRIMER.Enabled = True
                    BO_RECHERCHER.Enabled = True
                End If
                rs.Close()
            End If
        End If

    End Sub

    Private Function IsEleveOK() As Boolean

        IsEleveOK = False
        If TE_CD_CLI.TextLength = 0 Then
            Exit Function
        End If
        ' effectuer les controles sur l'élève
        If ControlerEleves("PLS", TE_CD_CLI.Text, gTypePlanning) = False Then
            Exit Function
        End If

        gCD_CLI = TE_CD_CLI.Text
        ' controler stage en double ?
        If IsEleve(gCD_CLI) Then
            szSQL = "CALL spGet_PLA_STAGE_LIGNES_byCD_CLI('" & gTypePlanning & "'," & gCD_CLI & "," & gSemaine & ");"
            GetRecordset(ctDSN_AEM, szSQL, rs)
            If rs.RecordCount > 0 Then
                MsgBox("L'élève est déjà inscrit au stage de la semaine " & Format(rs.Fields("semaine").Value, "0000-00") & ", groupe " & rs.Fields("groupe").Value, MsgBoxStyle.Critical)
                rs.Close()
                Exit Function
            End If
        End If

        ' Lire la fiche Elève
        szSQL = "CALL spGet_ELEVES_byCD_CLI(" & TE_CD_CLI.Text & ")"
        GetRecordset(ctDSN_AEM, szSQL, rs)
        If rs.RecordCount < 1 Then
            MsgBox("Elève " & TE_CD_CLI.Text & " non trouvé !", MsgBoxStyle.Critical)
            rs.Close()
            Exit Function
        End If

        TE_NOM.Text = rs.Fields("nompren").Value
        TE_FORMULE.Text = rs.Fields("formule").Value
        rs.Close()
        IsEleveOK = True

    End Function

    Private Sub BO_ENREGISTRER_Click(sender As System.Object, e As System.EventArgs) Handles BO_ENREGISTRER.Click
        Dim nDelai As Integer

        If IsEleveOK() = False Then
            TE_CD_CLI.Focus()
            Exit Sub
        End If

        ' oui, controler le solde de l'élève
        'nDelai = ControlerSolde("REEL", TE_CD_CLI.Text, TE_REF.Text)
        'If nDelai = -1 Then
        '    TE_CD_CLI.Focus()
        '    Exit Sub
        'End If
        nDelai = 0

        ' mise à jour 
        szSQL = "CALL spUpd_PLA_STAGE_LIG('" & gTypePlanning & "'," & gSemaine & ",'" & gGroupe & "'," & pCD_CLI & "," & _
             TE_CD_CLI.Text & ",'" & nq(TE_NOM.Text) & "','" & TE_LIEU.Text & "','" & getCodeFromCombo(CB_MONITEUR.SelectedItem) & "','" & _
             getCodeFromCombo(CB_STATUT.SelectedItem) & "'," & gUserAgence & "," & nDelai & ",'" & gUserCode & "')"
        If ExecQuery(ctDSN_AEM, szSQL) Then
            Me.Close()
        End If

    End Sub


    Private Sub BO_SUPPRIMER_Click(sender As System.Object, e As System.EventArgs) Handles BO_SUPPRIMER.Click

        ' utilisateur non manager ?
        gCD_CLI = TE_CD_CLI.Text
        If gUserAcces < 9 And IsEleve(gCD_CLI) Then
            ' oui, controler le délai annulation ?
            If IsDelaiOK(TE_DEBUT.Value, TE_MODIFIE_LE.Text, getCodeFromCombo(CB_STATUT.SelectedItem)) = False Then
                Exit Sub
            End If
        End If
        ' suppression
        deleteEnregistrement("pla_stage_lignes", TE_NOM.Text, gTypePlanning, gSemaine, gGroupe, TE_CD_CLI.Text)
        ' fermer la fenetre
        Me.Close()

    End Sub

    Private Sub BO_RECHERCHER_Click(sender As System.Object, e As System.EventArgs) Handles BO_RECHERCHER.Click
        ' si Elève non encore saisi, proposer le dernier code client consulté (gCD_CLI)
        If TE_CD_CLI.TextLength Then
            gCD_CLI = TE_CD_CLI.Text
        End If
        ' ouvre la fenetre de Recherche ELEVES
        frmElevesSearch.pType = "ELEVE"
        frmElevesSearch.ShowDialog()

        ' ancien cd_cli est de type élève, le nouveau doit être également élève
        If oldCD_CLI > 100000 And gCD_CLI < 100000 Then
            MsgBox("Vous devez sélectionner un élève !", MsgBoxStyle.Critical)
            Exit Sub
        End If
        TE_CD_CLI.Text = gCD_CLI
        TE_NOM.Text = frmElevesSearch.Nompren
        TE_FORMULE.Text = frmElevesSearch.Formule
        TE_REF.Text = frmElevesSearch.Ref_ins1
        ' si eleve non, interdire bouton "Fiche Eleve"
        If IsEleve(CInt(TE_CD_CLI.Text)) = False Then
            BO_ELEVE.Visible() = False
        Else
            BO_ELEVE.Visible() = True
        End If
        ' passe au champs suivant
        TE_LIEU.Focus()
    End Sub

    Private Sub BO_ELEVE_Click(sender As System.Object, e As System.EventArgs) Handles BO_ELEVE.Click
        ' affiche la fiche de l'éleve
        If TE_CD_CLI.TextLength Then
            gCD_CLI = TE_CD_CLI.Text
            frmElevesMAJ.ShowDialog()
        End If
    End Sub
End Class